package com.web.basic.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.web.basic.service.ElasticsearchService;
import com.web.common.basic.api.ElasticsearchApi;
import com.web.common.basic.elasticsearch.entity.ElasticIndexParam;
import com.web.common.basic.elasticsearch.entity.ElasticIndices;
import com.web.common.basic.elasticsearch.entity.ElasticParam;
import com.web.common.basic.entity.SimplePage;
import com.web.common.basic.elasticsearch.entity.ElasticDataListParam;
import com.web.common.result.R;
import com.web.common.result.ResultEnum;
import com.web.service.filter.xss.XssIgnore;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

@Tag(name = "elasticsearch功能")
@Controller
@RequestMapping(path = "/basic/elasticsearch")
public class ElasticsearchController implements ElasticsearchApi{

	@Autowired
	private ElasticsearchService elasticsearchServiceImpl;
	
	@XssIgnore
	@Operation(summary = "自定义操作")
	@Override
	public R<Object> customOperate(ElasticParam param, String from) {
		return elasticsearchServiceImpl.customOperate(param);
	}
	
	@Operation(summary = "获取所有索引")
	@Override
	public R<List<ElasticIndices>> getIndexList() {
		return elasticsearchServiceImpl.getIndexList();
	}
	
	@Operation(summary = "获取索引信息")
	@Override
	public R<Map<String, Object>> getIndex(ElasticIndexParam param) {		
		return elasticsearchServiceImpl.getIndex(param);
	}

	@Operation(summary = "创建索引")
	@Override
	public R<Object> createIndex(ElasticIndexParam param) {
		return elasticsearchServiceImpl.createIndex(param);
	}

	@Operation(summary = "更新索引的mapping")
	@Override
	public R<Object> updateIndexMapping(@Valid ElasticIndexParam param) {
		return elasticsearchServiceImpl.updateIndexMapping(param);
	}
	
	@Operation(summary = "获取索引存储的数据量")
	@Override
	public R<Integer> getIndexCount(ElasticIndexParam param) {
		R<Integer> json = new R<>();
		if(!StringUtils.hasText(param.getIndex())) {
			json.setResultEnum(ResultEnum.PARAMETER_ERROR);
			return json;
		}
		return elasticsearchServiceImpl.getIndexCount(param);
	}

	@Operation(summary = "删除索引信息")
	@Override
	public R<Object> deleteIndex(ElasticIndexParam param) {
		R<Object> json = new R<>();
		if(!StringUtils.hasText(param.getIndex())) {
			json.setResultEnum(ResultEnum.PARAMETER_ERROR);
			return json;
		}
		return elasticsearchServiceImpl.deleteIndex(param);
	}

	@Operation(summary = "获取数据列表")
	@Override
	public R<SimplePage> getDataList(ElasticDataListParam param) {
		if(!StringUtils.hasText(param.getJsonData()) || !StringUtils.hasText(param.getIndex())) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		return elasticsearchServiceImpl.getDataList(param);
	}
}
